package org.xy.yq.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xy.yq.entity.City;
import org.xy.yq.entity.Province;
import org.xy.yq.entity.User;
import org.xy.yq.service.ICityService;
import org.xy.yq.service.IProvinceService;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RequestMapping("/yq")
@Controller
public class YqController {
    @Autowired
    @Qualifier("provinceServiceImpl")
    private IProvinceService provinceService;
    @Autowired
    @Qualifier("cityServiceImpl")
    private ICityService cityService;
    @RequestMapping("/init")
    public String yqInit(){
        return "initYq";
    }
    @RequestMapping("/initToDb")
    public String initToDb(String json){
        System.out.println(json);
        List<Province> provinces = JSON.parseArray(json,Province.class);
        boolean flag =false;
        for (Province province : provinces) {
           flag = provinceService.insertProvince(province);
           flag = true;

        }
        return "/yq/init";
    }

    @RequestMapping(value = "/initCityToDb")
    public String initCityToDb(@RequestBody String json){
        System.out.println(json);
        List<City> cities = JSON.parseArray(json,City.class);
        boolean flag =false;
        for (City city : cities) {
            flag =cityService.insertCity(city);
        }
        return "initYq";
    }
    @RequestMapping("/china")
    public String china(){
        return "chinaYq";
    }
    @ResponseBody
    @RequestMapping("/getChina")
    public List<Province> getChina(){
        return provinceService.selectAll();
    }
    @RequestMapping("/province")
    public String province(){
        return "provinceYQ";
    }
    @ResponseBody
    @RequestMapping("/getCity")
    public List<City> getCity(String province){
        return  cityService.getCity(province);
    }
    @RequestMapping("/top5Pro")
    public String top5Province(){
        return "top5Pro";
    }
    @ResponseBody
    @RequestMapping("/getTop5Pro")
    public List<Province> getTop5Pro(){
        return provinceService.top5();
    }
    @RequestMapping("/top5City")
    public String top5City(){
        return "top5City";
    }
    @ResponseBody
    @RequestMapping("/getTop5City")
    public List<City> getTop5City(){
        return cityService.top5();
    }
    @RequestMapping("/toAll")
    public String toAllDate(){
        return "toAllData";
    }
}
